<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="sql.js"></script>
</head>
<body>
<script>
    // 引入fs 打开本地数据库
    var fs = require('fs');
    var filebuffer = fs.readFileSync('study02/test.db');// Load the db

    // 引用sql.js 实例化
    var db = new SQL.Database(filebuffer);

    // 两表关联查询
    var res2 = db.exec("SELECT a.*,b.id_name FROM student as a,cat as b where a.cid=b.id");
    alert(JSON.stringify(res2));

    // 创建数据表
    sqlstr = "CREATE TABLE hello (a int, b char);";
    sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
    sqlstr += "INSERT INTO hello VALUES (1, 'world');"
    db.run(sqlstr); // Run the query without returning anything

    // 查询表
    var res3 = db.exec("select * from hello");

    // 导出表，只要涉及 增、删、改，都需要此步操作
    var binaryArray = db.export();
    var buffer = new Buffer(binaryArray);
    fs.writeFileSync("study02/test.db", buffer);

    // Exportthe database to an Uint8Array containing the SQLite databasefile
//    varbinaryArray = db.export();
//    var buffer = new Buffer(binaryArray);
//    fs.writeFileSync("test.sqlite", buffer);
</script>
</body>
</html>